#include <conio.h>
#include <graphics.h>
#pragma comment(lib,"Winmm.lib")

#define high 600  // 游戏画面尺寸
#define width 350

// 全局变量
IMAGE img_bk, img_bird1, img_bird2, img_barup1, img_barup2, img_bardown1, img_bardown2;
int bird_x, bird_y;//小鸟位置
int barup_x, barup_y;
int bardown_x, bardown_y;
int highth;//通道高度
int score;
int k;

void startup()  // 数据初始化
{
	mciSendString("open E:\\background.mp3 alias bkmusic", NULL, 0, NULL);
	mciSendString("play bkmusic repeat", NULL, 0, NULL);
	k = 0;
	highth = 120;
	score = 0;
	initgraph(width, high);
	loadimage(&img_bk, "E:\\background.jpg");
	loadimage(&img_bird1, "E:\\bird1.jpg");
	loadimage(&img_bird2, "E:\\bird2.jpg");
	loadimage(&img_barup1, "E:\\bar_up1.gif");
	loadimage(&img_barup2, "E:\\bar_up2.gif");
	loadimage(&img_bardown1, "E:\\bar_down1.gif");
	loadimage(&img_bardown2, "E:\\bar_down2.gif");
	bird_x=90;
	bird_y=200;
	barup_x = 300;
	barup_y = -200-(rand()%2+1)*100;
	bardown_x = 300;
	bardown_y=600+barup_y+highth;
	
	BeginBatchDraw();
}

void clean()  // 显示画面
{
	// 绘制黑线、黑色填充的圆
	

}

void show()  // 显示画面
{
	putimage(0, 0, &img_bk);
	putimage(bird_x, bird_y,&img_bird1,NOTSRCERASE);
	putimage(bird_x, bird_y, &img_bird2,SRCINVERT);
	putimage(barup_x, barup_y, &img_barup1, NOTSRCERASE);
	putimage(barup_x, barup_y, &img_barup2, NOTSRCERASE);
	putimage(bardown_x, bardown_y, &img_bardown1, NOTSRCERASE);
	putimage(bardown_x, bardown_y, &img_bardown2, NOTSRCERASE);
	if ((bird_x >= bardown_x - 34 && bird_y >= bardown_y) || (bird_x >= barup_x - 34 && bird_y <= barup_y + 600)) {
		settextcolor(YELLOW);
		settextstyle(30, 0, _T("宋体"));
		outtextxy(120, 250, "游戏结束");
		k = 1;
	}
	FlushBatchDraw();
	// 延时
	Sleep(3);
	if (k == 1) {
		system("pause");
	}
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	if (bird_y < 580) {
		bird_y += 3;
	}
	if (barup_x > 1) {
		barup_x -= 3;
	}
	if (bardown_x > 1) {
		bardown_x -= 3;
	}
	if (bardown_x < 1) {
		barup_x = 300;
		barup_y = -200 - (rand() % 3 + 1) * 100;
		bardown_x = 300;
		bardown_y = 600 + barup_y + highth;
	}
	Sleep(40);
}

void updateWithInput()  // 与用户输入有关的更新
{
	char input;
	if (kbhit()) {
		input = getch();
		if (input == ' ') {
			bird_y -= 26;
			mciSendString("close jpmusic", NULL, 0, NULL);
			mciSendString("open E:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);
			mciSendString("play jpmusic", NULL, 0, NULL);
		}
	}
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		clean();  // 把之前绘制的内容清除
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
		show();  // 显示新画面
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}


